//
//  PopulationRegisterViewController.m
//  Utils
//
//  Created by enghou on 2018/4/20.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "PopulationRegisterViewController.h"
#import "NetWorkerManager.h"
#import "PopulationContainerViewController.h"
@interface PopulationRegisterViewController ()


@end

@implementation PopulationRegisterViewController

-(NSDictionary *)param{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if ([self.homeNumberField.text length] > 0) {
        [param setObject:self.homeNumberField.text forKey:@"population.accountNumber"];
    }else{
        [self alertSomes:@"请输入户号"];
        return nil;
    }
    if ([self.relationModel.ids length] > 0) {
        [param setObject:self.relationModel.ids forKey:@"population.relationShipWithHead.id"];
    }else{
        [self alertSomes:@"请选择与户主的关系"];
        return nil;
    }
    if ([self.stateModel.ids length] > 0) {
        [param setObject:self.stateModel.ids forKey:@"population.residentStatus.id"];
    }else{
        [self alertSomes:@"请选择人户一致标识"];
        return nil;
    }
    return param;
}

-(void)alertSomes:(NSString *)msg{
    PopulationContainerViewController *container = (PopulationContainerViewController *)self.theDelete;
    [container turnToIndex:[container.ctlArray indexOfObject:self] andAlert:msg];
}

-(void)selectRelations{
    if (!self.relations) {
        [self commonAlertWithContent:@"与户主关系" choosenBlock:^(PropertyDictItemModel *model) {
            self.relationModel = model;
            self.relationLabel.text = self.relationModel.displayName;
        } getList:^(NSArray *array) {
            self.relations = array;
        }];
    }else{
        [self alertWithArray:self.relations choosenBock:^(PropertyDictItemModel *model) {
            self.relationModel = model;
            self.relationLabel.text = self.relationModel.displayName;
        }];
    }
}

-(void)selectStates{
    if (!self.states) {
        [self commonAlertWithContent:@"人户状态" choosenBlock:^(PropertyDictItemModel *model) {
            self.stateModel = model;
            self.stateLabel.text = self.stateModel.displayName;
        } getList:^(NSArray *array) {
            self.states = array;
        }];
    }else{
        [self alertWithArray:self.relations choosenBock:^(PropertyDictItemModel *model) {
            self.stateModel = model;
            self.stateLabel.text = self.stateModel.displayName;
        }];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (!self.canEdit) {
        self.homeNumberField.enabled = NO;
    }
}


-(void)commonAlertWithContent:(NSString *)content choosenBlock:(void(^)(PropertyDictItemModel *))choosen getList:(void(^)(NSArray *))getList{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]getPropertyDictWithDomainName:content successBlock:^(BaseModel *model) {
        [self endLoading];
        if (model.success) {
            getList(model.list);
            [self alertWithArray:model.list choosenBock:choosen];
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(void)alertWithArray:(NSArray <PropertyDictItemModel *>*)array choosenBock:(void(^)(PropertyDictItemModel *))choosen{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    for (PropertyDictItemModel *model in array) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:model.displayName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            choosen(model);
        }];
        [alert addAction:action];
    }
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.canEdit) {
        if (indexPath.row == 1) {
            [self selectRelations];
        }else if (indexPath.row == 2){
            [self selectStates];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
